import Vue from 'vue'
import Router from 'vue-router'
import CookieUtil from '@/utils/cookies'
import {Message} from 'element-ui'
import store from './../store'
import Layout from '@/layout/Layout.vue'

Vue.use(Router)

const whiteList = [
    '/login'
]

let router = new Router({
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login/Index')
        },
        {
            path: '/',
            name: 'index',
            component: Layout,
            redirect: '/home',
            children: [
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('@/views/home/Index')
                },
                {
                    path: 'site/apply',
                    name: 'site-apply',
                    component: () => import('@/views/site/SiteApplyList'),
                },
                {
                    path: 'site/manage',
                    name: 'site-manage',
                    component: () => import('@/views/site/SiteList'),
                },
                {
                    path: 'site/cash',
                    name: 'site-cash',
                    component: () => import('@/views/site/SiteCashList'),
                },
                {
                    path: 'base',
                    name: 'base',
                    redirect: '/home',
                    component: () => import('@/views/base/Index'),
                    children: [
                        {
                            path: 'village',
                            name: 'base-village',
                            redirect: '/base/village/list',
                            component: () => import('@/views/base/village/Index'),
                            children: [
                                {
                                    path: 'list',
                                    name: 'base-village-list',
                                    meta: {
                                        index: "base-village"
                                    },
                                    component: () => import('@/views/base/village/List'),
                                },
                                {
                                    path: 'save',
                                    name: 'base-village-save',
                                    meta: {
                                        index: "base-village"
                                    },
                                    component: () => import('@/views/base/village/Save'),
                                }
                            ]
                        },
                        {
                            path: 'community',
                            name: 'base-community',
                            redirect: '/base/community/list',
                            component: () => import('@/views/base/community/Index'),
                            children: [
                                {
                                    path: 'list',
                                    name: 'base-community-list',
                                    meta: {
                                        index: "base-community"
                                    },
                                    component: () => import('@/views/base/community/List'),
                                },
                                {
                                    path: 'save',
                                    name: 'base-community-save',
                                    meta: {
                                        index: "base-community"
                                    },
                                    component: () => import('@/views/base/community/Save'),
                                }
                            ]
                        }
                    ]
                },
                {
                    path: 'goods',
                    name: 'goods',
                    redirect: '/goods/goods',
                    component: () => import('@/views/goods/Index'),
                    children: [
                        {
                            path: 'category',
                            name: 'goods-category',
                            component: () => import('@/views/goods/category/Category'),
                        },
                        {
                            path: 'purchase',
                            name: 'goods-purchase',
                            component: () => import('@/views/goods/goods/Purchase'),
                        },
                        {
                            path: 'goods',
                            name: 'goods-goods',
                            redirect: '/goods/goods/list',
                            component: () => import('@/views/goods/goods/Index'),
                            children: [
                                {
                                    path: 'list',
                                    name: 'goods-goods-list',
                                    meta: {
                                        index: "goods-goods"
                                    },
                                    component: () => import('@/views/goods/goods/List'),
                                },
                                {
                                    path: 'save',
                                    name: 'goods-goods-save',
                                    meta: {
                                        index: "goods-goods"
                                    },
                                    component: () => import('@/views/goods/goods/Save'),
                                },
                                {
                                    path: 'depot',
                                    name: 'goods-goods-depot',
                                    meta: {
                                        index: "goods-goods"
                                    },
                                    component: () => import('@/views/goods/goods/Depot'),
                                }
                            ]
                        }
                    ]
                },
                {
                    path: 'coupon',
                    name: 'coupon',
                    component: () => import('@/views/coupon/Index'),
                    children: [
                        {
                            path: 'config',
                            name: 'coupon-config',
                            component: () => import('@/views/coupon/Config'),
                        },
                        {
                            path: 'list',
                            name: 'coupon-list',
                            component: () => import('@/views/coupon/List'),
                        }
                    ]
                },
                {
                    path: 'activity',
                    name: 'activity',
                    component: () => import('@/views/activity/Index'),
                    children: [
                        {
                            path: 'coupon',
                            name: 'activity-coupon',
                            component: () => import('@/views/activity/coupon/List'),
                        },
                        {
                            path: 'promotion',
                            name: 'activity-promotion',
                            component: () => import('@/views/activity/promotion/List'),
                        }
                    ]
                },
                {
                    path: 'order/list',
                    name: 'order-list',
                    component: () => import('@/views/order/List')
                },
                {
                    path: 'order/return',
                    name: 'order-return',
                    component: () => import('@/views/order/OrderReturnList')
                },
                {
                    path: 'order/sales',
                    name: 'sales-statistics',
                    component: () => import('@/views/order/SalesStatistics')
                },
                {
                    path: 'employee/user',
                    name: 'employee-user',
                    component: () => import('@/views/employee/user/List')
                },
                {
                    path: 'verify/user',
                    name: 'verify-user',
                    component: () => import('@/views/verify/user/List')
                },
                {
                    path: 'system/config',
                    name: 'system-config',
                    component: () => import('@/views/system/SystemConfig'),
                },
                {
                    path: 'system/disconfig',
                    name: 'system-disconfig',
                    component: () => import('@/views/system/DisTimeConfig'),
                },
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (CookieUtil.isLogin()) {
        if (to.path === '/login') {
            next({path: '/', replace: true})
        } else if (to.path.indexOf('/error') >= 0) {
            next()
        } else {
            next()
            store.commit('setActiveIndex', to.meta.index ? to.meta.index : to.name)
        }
    } else {
        if (whiteList.indexOf(to.path) >= 0) {
            next()
        } else {
            let user = window.localStorage.getItem('manage_user')
            if (user) {
                window.localStorage.removeItem('manage_user')
                Message({message: '登录超时，请重新登录'})
            }
            next({path: '/login', replace: true})
        }
    }
})

export default router
